﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
//Aforge
using AForge;
using AForge.Imaging;
using AForge.Imaging.Filters;
//Xna Texture2D
using Microsoft.Xna.Framework.Graphics;

namespace Puzzle_3D
{
    class Puzzle_ImageProcessing
    {
        private static int SCALEWIDTH = 900;
        private static int SCALEHEIGHT = 600;
        private static int PUZZLEWIDTH = 300;
        private static int PUZZLEHEIGHT = 300;
        private static String IMAGEPATH = System.Threading.Thread.GetDomain().BaseDirectory + "Bilder";

        private Puzzle root;

        //Filter
        private ResizeBilinear scaleFilter;
        private Sepia sepia;
        private OilPainting oilPainting;

        public Puzzle_ImageProcessing(Puzzle _root)
        {
            this.root = _root;
            this.scaleFilter = new ResizeBilinear(SCALEWIDTH, SCALEHEIGHT);
            this.sepia = new Sepia();
            this.oilPainting = new OilPainting(6);
        }

        public String scaleImageDown(String _path)
        {
            Bitmap tempImage = AForge.Imaging.Image.FromFile(_path);
            if (tempImage.Width > SCALEWIDTH || tempImage.Height > SCALEHEIGHT)
            {
                Bitmap scaledImage = this.scaleFilter.Apply(tempImage);
                _path = IMAGEPATH + "\\new_" + _path.Substring(_path.LastIndexOf("\\")+1);
                scaledImage.Save(_path);
                //Speicher freigeben
                tempImage.Dispose();
                scaledImage.Dispose();
            }
            return _path;
        }

        public void cutImageIntoPieces(Puzzle_Image _image, int _option, int _pieceCount)
        {
            Bitmap sourceImage = new Bitmap(_image.getFullImagePath());
            Texture2D[] pieces = new Texture2D[_pieceCount];
            int index = 0;
            int xPos = 0;
            int yPos = 0;
            Crop cropRect = new Crop(new Rectangle(xPos, yPos, PUZZLEWIDTH, PUZZLEHEIGHT));
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    Bitmap tempPiece = cropRect.Apply(sourceImage);
                    if (_option == 1)
                    {
                        //Grayscale
                        tempPiece = Grayscale.CommonAlgorithms.BT709.Apply(tempPiece);
                    }
                    else if (_option == 2)
                    {
                        //Sepia
                        this.sepia.ApplyInPlace(tempPiece);
                    }
                    else if (_option == 3)
                    {
                        //OilPainting
                        this.oilPainting.ApplyInPlace(tempPiece);
                    }
                    pieces[index] = this.convertBmpToTexture2D(tempPiece);
                    tempPiece.Dispose();

                    xPos += PUZZLEWIDTH;
                    index++;
                    cropRect.Rectangle = new Rectangle(xPos, yPos, PUZZLEWIDTH, PUZZLEHEIGHT);
                }
                xPos = 0;
                yPos = 300;
                cropRect.Rectangle = new Rectangle(xPos, yPos, PUZZLEWIDTH, PUZZLEHEIGHT);
            }
            _image.setImagePieces(pieces);
        }

        public Texture2D convertBmpToTexture2D(Bitmap _image)
        {
            Texture2D tempTexture = null;
            using (System.IO.MemoryStream s = new System.IO.MemoryStream())
            {
                _image.Save(s, System.Drawing.Imaging.ImageFormat.Png);
                s.Seek(0, System.IO.SeekOrigin.Begin);
                tempTexture = Texture2D.FromFile(this.root.getGraphics().GraphicsDevice, s);
            }
            return tempTexture;
        }
    }
}